﻿
// GameCodeDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "GameCode.h"
#include "GameCodeDlg.h"
#include "afxdialogex.h"
#include "IniFile.h"
#include "StdioFileEx.h"
#include "tlhelp32.h"
#include <winsock2.h> 
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//配置文件路径
#define ConfigFile  _T(".//Config.ini")
#define AccountFile _T(".//Account.ini")
CIniFile g_AccountFile;
CIniFile g_IniFile;

CString arrState[] = { L"未登录",L"游戏在线",L"账号异常" };
HWND   g_main_hwnd = NULL;
CGameCodeDlg* g_main_cwnd = NULL;
bool StartLoginGame(int nIndex,long hwnd);
int GetCodeIndex();
CString sState = L"";
// CGameCodeDlg 对话框

bool bState = false;//是否开启游戏

int gState = 0;//未登录,1.已登录 2.账号错误

CGameCodeDlg::CGameCodeDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_GAMECODE_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGameCodeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_INFO, m_list_info);
}

BEGIN_MESSAGE_MAP(CGameCodeDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_START, &CGameCodeDlg::OnBnClickedBtnStart)
	ON_BN_CLICKED(IDC_BTN_IMPORT, &CGameCodeDlg::OnBnClickedBtnImport)
	ON_BN_CLICKED(IDC_BUTTON1, &CGameCodeDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CGameCodeDlg::OnBnClickedButton2)
END_MESSAGE_MAP()

void StringSplit(CString source, CStringArray& dest, char division)
{
	if (source.IsEmpty())
		return;
	dest.RemoveAll();
	for (int i = 0; i < source.GetLength(); i++)
	{
		if (source.GetAt(i) == division)
		{
			dest.Add(source.Left(i));
			source.Delete(0, i + 1);
			i = 0;
		}
	}
	dest.Add(source);
}


CString NumberToString(LONG Number)
{
	CString sTring;
	sTring.Format(_T("%ld"), Number);
	return sTring;
}

void KillProcess(CString sExeName)
{
	HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnapShot == 0)
		return;
	PROCESSENTRY32 thePE;
	thePE.dwSize = sizeof(PROCESSENTRY32);


	//遍历正在运行的第一个系统进程


	bool Status = Process32First(hSnapShot, &thePE);
	bool bHaveFlag = false;
	DWORD ProcessID = 0;


	while (Status)
	{
		//遍历正在运行的下一个系统进程  
		Status = Process32Next(hSnapShot, &thePE);
		//找到相应的进程 **.exe


		// if(0 == wcscmp(thePE.szExeFile,L""))
		CString sFindName = thePE.szExeFile;
		CString sTemp = sExeName.Mid(0, sFindName.GetLength());


		if (sFindName == sTemp)
		{
			bHaveFlag = true;
			ProcessID = thePE.th32ProcessID;


			////结束指定的进程 ProcessID
			if (!TerminateProcess(OpenProcess(PROCESS_TERMINATE || PROCESS_QUERY_INFORMATION, false, ProcessID), 0))
			{
				//AfxMessageBox(L"无法终止指定的进程！", MB_ICONWARNING || MB_OK);
			}
			else
			{
				//AfxMessageBox(L"进程结束！", MB_ICONWARNING || MB_OK);
				break;
			}
		}
	}
	CloseHandle(hSnapShot);
}

DWORD RandomNumber(DWORD dwNumber1, DWORD dwNumber2)
{
	DWORD dwTemp;
	__asm
	{
		push ecx
		push edx
		rdtsc
		mov ecx, 0x17
		mul ecx
		add eax, 0x7
		mov ecx, dwNumber2//ss:[ebp + 0c]
		sub ecx, dwNumber1//ss : [ebp + 8]
		inc ecx
		xor edx, edx
		div ecx
		add edx, dwNumber1//ss : [ebp + 8]
		mov dwTemp, edx
		pop edx
		pop ecx
	}
	return dwTemp;
}
// CGameCodeDlg 消息处理程序

BOOL CGameCodeDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	ListView_SetExtendedListViewStyle(m_list_info.GetSafeHwnd(),
		LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP | LVS_EX_GRIDLINES);    //网格线 //LVS_EX_CHECKBOXES
	int nIndex = 0;
	m_list_info.InsertColumn(nIndex++, _T("序号"), LVCFMT_CENTER, 40);//0
	m_list_info.InsertColumn(nIndex++, _T("账号"), LVCFMT_LEFT, 80);//2
	m_list_info.InsertColumn(nIndex++, _T("密码"), LVCFMT_LEFT, 80);//3
	m_list_info.InsertColumn(nIndex++, _T("二密"), LVCFMT_CENTER, 80);//4
	m_list_info.InsertColumn(nIndex++, _T("服务器"), LVCFMT_CENTER, 80);//4
	m_list_info.InsertColumn(nIndex++, _T("上号时间"), LVCFMT_CENTER, 80);//4
	g_IniFile.SetIniFileName(ConfigFile);
	g_AccountFile.SetIniFileName(AccountFile);

	//启动时检查配置文件是否存在



	SetDlgItemTextW(IDC_EDIT3, L"-1");


	g_main_cwnd = this;

	CStringArray Sections;

	int iCount = g_AccountFile.GetSections(Sections);

	CString str; //获取系统时间 　　
	CTime tm; tm = CTime::GetCurrentTime();
	str = tm.Format("%Y%m%d");
	CString sDate = g_IniFile.GetString(L"Set", L"AppDate");

	for (int i = 0; i < iCount; i++)
	{
		int ItemCount = m_list_info.InsertItem(m_list_info.GetItemCount(), NumberToString(i));
		m_list_info.SetItemText(ItemCount, 1, g_AccountFile.GetString(NumberToString(i + 1), _T("code")));
		m_list_info.SetItemText(ItemCount, 2, g_AccountFile.GetString(NumberToString(i + 1), _T("password")));
		m_list_info.SetItemText(ItemCount, 3, g_AccountFile.GetString(NumberToString(i + 1), _T("twocode")));
		m_list_info.SetItemText(ItemCount, 4, g_AccountFile.GetString(NumberToString(i + 1), _T("server")));
		m_list_info.SetItemText(ItemCount, 5, g_AccountFile.GetString(NumberToString(i + 1), _T("time")));


	}



	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CGameCodeDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CGameCodeDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

unsigned WINAPI MainThread(PVOID pParam)
{
		// TODO: 在此添加控件通知处理程序代码
	long hwnd,x,y;
	int ret=0;
	int nState = 0,j=0,i=0;
	int nIndex = 0;
	sState = L"未登陆";
	CString str1;
	while (true)
	{
		if (sState == L"未登陆") {
			//取登陆账号					g_main_cwnd->AppendText(str1);
			ret = 0;
			g_dm->UnBindWindow();
			str1.Format(L"当前状态%d：开启游戏", gState);
			g_main_cwnd->AppendText(str1);
			g_dm->MoveTo(0, 1);
			g_dm->LeftClick();
			hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");
			if (hwnd > 0) {
				KillProcess(L"MapleStory.exe");
				g_dm->delay(3000);
			}
			g_dm->delay(2000);
			hwnd = g_dm->FindWindow(L"", L"MapleStory");
			for (size_t i = 0; i < 5; i++)
			{
				g_dm->FindPic(0, 0, 500, 960, L"game.bmp", L"202020", 0.8, 0, &x, &y);
				if (x > 0) {
					g_dm->MoveTo(x, y);
					g_dm->LeftDoubleClick();
					break;
				}
				Sleep(1000);
			}
			Sleep(1000);
			hwnd = g_dm->FindWindow(L"", L"MapleStory");//查找客户端句柄
			g_dm->SetWindowState(hwnd, 12);
			Sleep(1000);
			g_dm->KeyPress(9);
			Sleep(1000);
			g_dm->KeyPress(13);
			Sleep(10000);
			g_dm->LeftClick();
			for (int i = 0; i < 10; i++)
			{
				hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");
				if (hwnd > 0) {
					Sleep(10000);
					sState = L"启动游戏";
					Sleep(10000);
					str1.Format(L"点击开始");
					g_main_cwnd->AppendText(str1);
					break;
				}
				Sleep(10000);
			}
			i++;
			if (i > 5) {
				i = 0;
				sState = L"未登陆";
			}
		}
		else if (sState == L"启动游戏") {
			if (hwnd > 0 && ret == 0) {
				str1.Format(L"当前状态%d：找到游戏客户端", gState);
				g_main_cwnd->AppendText(str1);
				g_dm->UnBindWindow();
				ret = g_dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);
				str1.Format(L"绑定%d", ret);
				g_main_cwnd->AppendText(str1);
			}
			if (ret == 1) {
				g_dm->FindPic(0, 0, 800, 600, L"gameLogo.bmp|code.bmp|password.bmp", L"202020", 0.9, 0, &x, &y);
				if (x > 0) {
					g_dm->SetWindowState(hwnd, 12);
					g_dm->MoveWindow(hwnd, 0, 0);
					str1.Format(L"当前状态%d：游戏开启正在登录", gState);
					g_main_cwnd->AppendText(str1);
					nIndex = GetCodeIndex();
					g_main_cwnd->AppendText(L"当前账号:" + g_main_cwnd->m_list_info.GetItemText(nIndex, 1));
					if (StartLoginGame(nIndex, hwnd)) {
						str1.Format(L"当前状态%d：游戏登录成功", gState);
						g_main_cwnd->AppendText(str1);
						sState = L"登陆成功";
						g_main_cwnd->SetDlgItemTextW(IDC_EDIT3, g_main_cwnd->m_list_info.GetItemText(nIndex, 5));
						ret = 0;
					}
					else {
						i = 0;
						sState = L"未登陆";
					}
				}
			}
			i++;
			if (i > 10) {
				i = 0;
				sState = L"未登陆";
				g_main_cwnd->AppendText(L"登陆次数大于10重新登陆");
			}
			str1.Format(L"开启游戏次数%d", i);
			g_main_cwnd->AppendText(str1);

		}
		else if (sState ==L"登陆成功") {
			str1.Format(L"当前账号索引:%d,上号时间：%s", nIndex, g_main_cwnd->m_list_info.GetItemText(nIndex, 5));
			g_main_cwnd->AppendText(str1);
			i = 0;
			hwnd = (long)FindWindowW(L"MapleStoryClass", L"MapleStory");
			if (hwnd == 0) {
				sState = L"未登陆";
			}


		}
		str1.Format(L"当前状态:%s", sState);
		g_main_cwnd->AppendText(str1);


		//g_dm->FindPic(0, 0, 1280, 960, L"ngs.bmp", L"050505", 0.9, 0, &x, &y);
		//if (x > 0) {
		//	g_dm->MoveTo(x, y);
		//	g_dm->LeftClick();
		//	g_dm->KeyPress(13);
		//}
		//CString str1;
		//nState = 0;
		//while (gState==0) {
		//	CString sTime = g_IniFile.GetString(L"Set", L"RunTime");
		//	//取当前日期
		//	CString str; //获取系统时间 　　
		//	CTime tm;
		//	tm = CTime::GetCurrentTime();
		//	CString sTime1 = tm.Format("%H");
		//	if (sTime1 == L"00") {
		//		sTime1 = L"24";
		//	}
		//	if (sTime == L"0") {
		//		sTime = L"24";
		//	}
		//	if (sTime == L"") {//如果配置文件的日期为空
		//		//写入当前配置文件时间
		//		if (_wtoi(sTime1) % 2 == 1) {
		//			g_IniFile.SetInt(L"Set", L"RunTime", _wtoi(sTime1)-1);
		//			str.Format(L"%d", _wtoi(sTime1) - 1);
		//			//nIndex = GetCodeIndex(str);
		//		}
		//		else {
		//			g_IniFile.SetInt(L"Set", L"RunTime", _wtoi(sTime1));
		//			//nIndex = GetCodeIndex(sTime1);
		//		}
		//		
		//	}
		//	else {//配置文件存在日期
		//		//如果当前状态为假
		//		if (gState == 0) {
		//			//当前时间 3，4
		//			if (_wtoi(sTime1) != _wtoi(sTime) && abs((_wtoi(sTime1) - _wtoi(sTime))) % 2 == 1) {
		//				
		//				if (_wtoi(sTime1) - _wtoi(sTime) >= 3) {
		//					int nTime = _wtoi(sTime)+ (_wtoi(sTime1) - _wtoi(sTime)-1);
		//					//nIndex = GetCodeIndex(NumberToString(nTime));
		//					g_IniFile.SetInt(L"Set", L"RunTime", nTime);
		//				}
		//				else {
		//					//nIndex = GetCodeIndex(sTime);//取配置时间
		//				}
		//				
		//			}
		//			else {
		//				int nTime = _wtoi(sTime1);
		//				//nIndex = GetCodeIndex(NumberToString(nTime));//当前时间
		//				g_IniFile.SetInt(L"Set", L"RunTime", nTime);
		//			}
		//		}
		//	}
		//	str1.Format(L"当前登录索引%d：开启游戏", nIndex);
		//	g_main_cwnd->AppendText(str1);
		//	//登录游戏
		//	if (nState == 0) {
		//		str1.Format(L"登录状态%d", nState);
		//		g_main_cwnd->AppendText(str1);
		//		ret = 0;
		//		g_dm->UnBindWindow();
		//		str1.Format(L"当前状态%d：开启游戏", gState);
		//		g_main_cwnd->AppendText(str1);
		//		g_dm->MoveTo(0, 1);
		//		g_dm->LeftClick();
		//		hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");
		//		if (hwnd > 0) {
		//			KillProcess(L"MapleStory.exe");
		//			g_dm->delay(1000);
		//		}
		//		g_dm->delay(1000);
		//		hwnd = g_dm->FindWindow(L"", L"MapleStory");
		//		for (size_t i = 0; i < 5; i++)
		//		{
		//			g_dm->FindPic(0, 0, 500, 960, L"game.bmp", L"202020", 0.8, 0, &x, &y);
		//			if (x > 0) {
		//				g_dm->MoveTo(x, y);
		//				g_dm->LeftDoubleClick();
		//				break;
		//			}
		//			Sleep(1000);
		//		}
		//		Sleep(1000);
		//		hwnd = g_dm->FindWindow(L"", L"MapleStory");//查找客户端句柄
		//		g_dm->SetWindowState(hwnd, 12);
		//		Sleep(1000);
		//		g_dm->KeyPress(9);
		//		Sleep(1000);
		//		g_dm->KeyPress(13);
		//		Sleep(10000);
		//		g_dm->LeftClick();

		//		for (int i = 0; i < 10; i++)
		//		{
		//			hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");
		//			if (hwnd>0) {
		//				Sleep(10000);
		//				nState = 1;
		//				Sleep(10000);
		//				str1.Format(L"点击开始");
		//				g_main_cwnd->AppendText(str1);
		//				break;
		//			}
		//			Sleep(10000);
		//		}
		//	}else if (nState == 1)
		//	{
		//		//hwnd = g_dm->FindWindow(L"", L"MapleStory");//查找客户端句柄
		//		//if (hwnd > 0) {
		//		//	//state = 1;
		//		//}
		//		//查找登录窗口
		//		Sleep(1000);
		//		str1.Format(L"当前状态-%d,开启游戏,次数%d", gState,j);
		//		g_main_cwnd->AppendText(str1);

		//		hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");
		//		str1.Format(L"登录状态%d,绑定状态%d,次数%d", nState,ret,j);

		//		g_main_cwnd->AppendText(str1);
		//		if (hwnd > 0 && ret == 0) {
		//			str1.Format(L"当前状态%d：找到游戏客户端", gState);
		//			g_main_cwnd->AppendText(str1);
		//			g_dm->UnBindWindow();
		//			ret = g_dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);
		//			str1.Format(L"绑定%d", ret);
		//			g_main_cwnd->AppendText(str1);
		//		}
		//		if (ret == 1) {
		//			//是否进入游戏
		//			g_dm->FindPic(0, 0, 800, 600, L"gameLogo.bmp|code.bmp|password.bmp", L"202020", 0.9, 0, &x, &y);
		//			if (x > 0) {
		//				g_dm->SetWindowState(hwnd, 12);
		//				g_dm->MoveWindow(hwnd, 0, 0);
		//				str1.Format(L"当前状态%d：游戏开启正在登录", gState);
		//				g_main_cwnd->AppendText(str1);
		//				if (StartLoginGame(nIndex, hwnd)) {
		//					str1.Format(L"当前状态%d：游戏登录成功", gState);
		//					g_main_cwnd->AppendText(str1);
		//					gState = 1;
		//					//nState = 0;
		//					ret = 0;
		//				}
		//			}
		//		}

		//		if (j > 200) {
		//			nState = 0;
		//		}

		//	}
		//	j++;
		//}
		Sleep(1000);
	}

}

unsigned WINAPI SubThread(PVOID pParam) {
	int i = 0;
	CString str;
	long x, y;
	while (true)
	{
		CTime tm;
		tm = CTime::GetCurrentTime();
		CString sTime1 = tm.Format("%H");
		int nTime = _wtoi(sTime1);
		if (nTime ==0) {
			nTime = 24;
		}
		if (nTime%2 == 0) {
			tm = CTime::GetCurrentTime();
			CString sM = tm.Format("%M");
			if (_wtoi(sM) == 0) {
				sState = L"未登陆";
				Sleep(61 * 1000);
			}

		}
		Sleep(2000);
		
		//g_dm->FindPic(0, 0, 1280, 960, L"ngs.bmp", L"050505", 0.9, 0, &x, &y);
		//if (x > 0) {
		//	sState = L"未登陆";
		//	g_dm->MoveTo(x, y);
		//	g_dm->LeftClick();
		//	g_dm->KeyPress(13);
		//}

		//if (gState == 1) {
		//	int hwnd;
		//	if (gState == 1) {//游戏登录后检测窗口是否掉线

		//		hwnd = (long)FindWindowW(L"MapleStoryClass", L"MapleStory");
		//		if (hwnd == 0) {
		//			gState = 0;
		//		}
		//	}
		//}
		//if (gState > 0) {
		//	//检测日期
		//	int nTime = g_IniFile.GetInt(L"Set", L"RunTime");
		//	CTime tm;
		//	tm = CTime::GetCurrentTime();
		//	CString sTime1 = tm.Format("%H");
		//	if (nTime == 0) {
		//		nTime = 24;
		//	}
		//	int nTime1 = _wtoi(sTime1);
		//	if (nTime1 == 0) {
		//		nTime1 = 24;
		//	}
		//	if (nTime1 != nTime && (nTime1-nTime)%2 == 0 || abs(nTime1-nTime)>=3) {
		//		if (abs(nTime1 - nTime) <= 22) {
		//			gState = 0;
		//		}
		//	}
		//}
		//Sleep(1000);
		//i++;
		//if (i % 20 == 0) {
		//	if (gState == 0) {
		//		str.Format(L"当前状态%d：游戏正在登录", gState);
		//	}
		//	else if (gState == 1) {
		//		str.Format(L"当前状态%d：游戏正常", gState);			}
		//	else if (gState == 2) {
		//		str.Format(L"当前状态%d：账号异常", gState);
		//	}
		//	g_main_cwnd->AppendText(str);
		//}

		//g_dm->FindPic(0, 0, 1280, 960, L"ngs.bmp", L"050505", 0.9, 0, &x, &y);
		//if (x > 0) {
		//	g_dm->KeyPress(13);
		//}
	
	}

}

void CGameCodeDlg::OnBnClickedBtnStart()
{
	_beginthreadex(0, 0, MainThread, NULL, 0, 0);
	Sleep(1000);
	_beginthreadex(0, 0, SubThread, NULL, 0, 0);


}




bool StartLoginGame(int nIndex,long hwnd){
	
	long x, y;
	int z = 0;
	bool flag = false;
	Sleep(5000);
	g_dm->SetWindowState(hwnd, 12);
	g_dm->SetDict(0, L"code.txt");
	g_dm->UseDict(0);
	while (gState==0)
	{

		g_dm->FindPic(288, 220, 510, 301, L"onLineCode.bmp", L"202020", 0.8, 0, &x, &y);
		if (x> 0) {
			g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"5");
			gState = 2;
			KillProcess(L"MapleStory.exe");
			//return true;
			AfxMessageBox(L"账号在线");
		}
		Sleep(2000);
		g_dm->FindPic(300, 226, 487, 288, L"codeError.bmp|codeError.bmp", L"202020", 0.8, 0, &x, &y);
		if (x > 0) {
			g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"2");
			gState = 2;
			KillProcess(L"MapleStory.exe");
			//return true;
			AfxMessageBox(L"账号密码错误");
		}
		Sleep(2000);
		g_dm->FindPic(331, 251, 463, 306, L"twoPwdError.bmp", L"202020", 0.8, 0, &x, &y);
		if (x > 0) {
			g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"3");
			gState = 2;
			KillProcess(L"MapleStory.exe");
			//return true;
			AfxMessageBox(L"二密错误");
		}
		g_dm->MoveToEx(100, 100, 30, 45);

		if (g_IniFile.GetString(L"Set", L"kou") == L"1") {//登录入口
			Sleep(2000);
			g_dm->FindPic(265, 234, 395, 280, L"loginTag.bmp", L"202020", 0.8, 0, &x, &y);
			if (x > 0) {
				g_dm->MoveTo(x, y);
				Sleep(500);
				g_dm->LeftClick();
				Sleep(2000);
			}
			
		}

		g_dm->FindPic(269, 272, 450, 308, L"loginCode.bmp|loginCode1.bmp|code.bmp", L"202020", 0.8, 0, &x, &y);
		if (x > 0) {
			g_dm->MoveTo(x+10, y+5);
			Sleep(500);
			g_dm->LeftClick();
			Sleep(1500);
			g_dm->SendString2(hwnd, g_main_cwnd->m_list_info.GetItemText(nIndex, 1));
			Sleep(2000);
		}
		g_dm->FindPic(277, 308, 455, 333, L"loginPwd.bmp|password.bmp", L"202020", 0.8, 0, &x, &y);
		if (x > 0) {
			g_dm->MoveTo(x + 10, y + 3);
			Sleep(500);
			g_dm->LeftClick();
			Sleep(1500);
			g_dm->SendString2(hwnd, g_main_cwnd->m_list_info.GetItemText(nIndex, 2));
			Sleep(2000);
			g_dm->FindPic(440, 290, 490, 320, L"login.bmp", L"202020", 0.8, 0, &x, &y);
			if (x > 0) {
				g_dm->MoveTo(x + 10, y + 3);
				Sleep(500);
				g_dm->LeftClick();
				Sleep(3000);
			}
		}

		
		
		g_dm->FindPic(325, 270, 365, 307, L"checkCode.bmp", L"202020", 0.8, 0, &x, &y);
		if (x > 0) {
			g_dm->MoveTo(x + 30, y + 46);
			Sleep(500);
			g_dm->LeftDoubleClick();
			Sleep(3000);
		}
		CString sServer;
		sServer.Format(L"loginChoseServer%s.bmp", g_main_cwnd->m_list_info.GetItemText(nIndex, 4));
		g_dm->FindPic(655, 64, 771, 539, sServer, L"303030", 0.80, 0, &x, &y);
		if (x > 0) {
			if (g_main_cwnd->m_list_info.GetItemText(nIndex, 4) == L"6") {
				//dm->MoveTo(711, 129);
				g_dm->MoveToEx(711, 125, 20, 5);
			}
			else if (g_main_cwnd->m_list_info.GetItemText(nIndex, 4) == L"5") {
				g_dm->MoveToEx(711, 160, 20, 5);
			}
			else
			{
				g_dm->MoveTo(x + 50, y + 18);
			}
			Sleep(1000);
			g_dm->LeftClick();
		}
		


		
		//CString sServer;
		//sServer.Format(L"loginChoseServer%s.bmp", g_main_cwnd->m_list_info.GetItemText(nIndex, 4));
		//g_dm->FindPic(655, 64, 771, 539, sServer, L"303030", 0.80, 0, &x, &y);
		//if (x > 0) {
		//	if (g_main_cwnd->m_list_info.GetItemText(nIndex, 4) == L"6") {
		//		//dm->MoveTo(711, 129);
		//		g_dm->MoveToEx(711, 125, 20, 5);
		//	}
		//	else if (g_main_cwnd->m_list_info.GetItemText(nIndex, 4) == L"5") {
		//		g_dm->MoveToEx(711, 160, 20, 5);
		//	}
		//	else
		//	{
		//		g_dm->MoveTo(x + 50, y + 18);
		//	}

		//	Sleep( 1000);
		//	g_dm->LeftClick();
		//}

		g_dm->FindPic(200, 390, 282, 420, L"loginServer25.bmp", L"303030", 0.8, 0, &x, &y);
		if (x > 0) {
			g_dm->MoveTo(x, y);
			Sleep(1500);
			g_dm->LeftDoubleClick();
			Sleep(2000);
		}


		g_dm->FindPic(620, 390, 725, 435, L"twoCodeWnd.bmp", L"202020", 0.8, 0, &x, &y);
		if (x > 0) {
			g_dm->MoveTo(x , y);
			Sleep(500);
			g_dm->LeftClick();
			Sleep(3000);
		}

		g_dm->FindPic(123, 200, 241, 219, L"twoCodeKey.bmp", L"202020", 0.8, 0, &x, &y);
		if (x > 0 && !flag) {
			CString code = g_main_cwnd->m_list_info.GetItemText(nIndex, 3);
			CString code1;
			Sleep(3000);
			for (size_t i = 0; i < 2; i++)
			{
				Sleep(5000);
				g_dm->MoveToEx(200, 100, 100, 100);
				Sleep(5000);
			}

			for (int i = 0; i < code.GetLength(); i++)
			{

				Sleep(1500);
				code1 = code1 + code.Mid(i, 1);
				CString strbmp = code.Mid(i, 1) + L".bmp";
				CString sTwoCode = L"输入二密" + code.Mid(i, 1);

				g_dm->FindPic(126, 222, 675, 417, strbmp, L"202020", 0.90, 0, &x, &y);
				long curX, curY;
				if (x > 0) {
					x = x + RandomNumber(5, 5) + 10;
					y = y + RandomNumber(5, 5) + 5;
					g_dm->MoveTo(x, y);
					Sleep(500);
					for (size_t i = 0; i < 5; i++)
					{
						g_dm->GetCursorPos(&curX, &curY);
						Sleep(100);
						if (curX != x) {
							Sleep(1500);
							g_dm->MoveTo(x, y);
						}
					}

					g_dm->LeftClick();
					Sleep(1000);
				}
				g_dm->MoveToEx(RandomNumber(200, 300), 100, 30, 45);
			}
			for (int i = 0; i < 5; i++)
			{
				g_dm->FindPic(298, 415, 399, 441, L"enterCode.bmp", L"303030", 0.85, 0, &x, &y);
				if (x > 0) {
					g_dm->MoveToEx(x, y, 10, 5);
					g_dm->LeftClick();
					g_dm->delay(1500);
					flag = true;
					break;
				}
			}


		}

		g_dm->FindPic(281, 213, 524, 350, L"twoCodeError.bmp", L"303030", 0.9, 0, &x, &y);
		if (x > 0) {
			//TODO: 二密错误 处理 
			g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"3");
			g_main_cwnd->m_list_info.SetItemText(nIndex, 6, L"二密错误");
			KillProcess(L"MapleStory.exe");
			Sleep(3000);
			gState = 2;
			return false;
		}
		if (flag) {
			Sleep(1500);

			g_dm->FindPic(767, 720, 803, 754, L"loginSuccess.bmp|loginSuccess1.bmp", L"303030", 0.9, 0, &x, &y);
			{
				if (x> 0) {
					return true;
				}
			}
		}

		z++;
		CString str1;
		str1.Format(L"登陆次数%d", z);
		g_main_cwnd->AppendText(str1);
		if (z > 10) {
			str1.Format(L"退出登录");
			g_main_cwnd->AppendText(str1);
			return false;
		}
		Sleep(500);
	}
}

void CGameCodeDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::OnCancel();
}


void CGameCodeDlg::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类

	//CDialogEx::OnOK();
}


BOOL CGameCodeDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类

	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_RETURN://屏蔽回车
			return TRUE;
		case VK_ESCAPE://屏蔽Esc
			return TRUE;
		}
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}

//导入账号
void CGameCodeDlg::OnBnClickedBtnImport()
{
	// TODO: 在此添加控件通知处理程序代码

	CString strFileName, strPathName;

	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR, _T("文本文件(*.txt)|*.txt||"));
	if (dlg.DoModal() == IDOK)
	{
		strFileName = dlg.GetPathName();
		CStdioFileEx sFile;
		CStringArray ArrAcountInfo;
		if (!strFileName.IsEmpty())
		{
			sFile.Open(strFileName, CFile::modeRead);
			CString strLine;
			CString str; //获取系统时间 　　
			CTime tm; 
			tm = CTime::GetCurrentTime();
			
			//str = tm.Format("%H");
			int nTime1 = _wtoi(tm.Format("%H"));
			CString sDate = g_IniFile.GetString(L"Set", L"AppDate");
			int i = 0;
			while (sFile.ReadString(strLine))
			{

				CString strTemp = strLine;
				int len = strLine.GetLength();
				if (len > 100)
				{
					sFile.Close();
					return;
				}
				//zhanghao-mima-daqu
				strLine.Replace(_T("----"), _T("-"));
				strLine.Replace(_T("---"), _T("-"));
				strLine.Replace(_T("--"), _T("-"));

				StringSplit(strLine, ArrAcountInfo, '-');

				int	iBegin = m_list_info.GetItemCount();

				int iRow = m_list_info.InsertItem(iBegin, NumberToString(iBegin));
				m_list_info.SetItemText(iRow, 1, ArrAcountInfo[0]);//瑯뵀
				m_list_info.SetItemText(iRow, 2, ArrAcountInfo[1]);//쵱쯤
				m_list_info.SetItemText(iRow, 3, ArrAcountInfo[2]);//댕혐
				m_list_info.SetItemText(iRow, 4, ArrAcountInfo[3]);//댕혐
				m_list_info.SetItemText(iRow, 5, NumberToString(i*2));
				if (!ArrAcountInfo[0].IsEmpty() && !ArrAcountInfo[1].IsEmpty())//&& !ArrAcountInfo[2].IsEmpty())
				{
					g_AccountFile.SetString(NumberToString(iRow + 1), L"code", ArrAcountInfo[0]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"password", ArrAcountInfo[1]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"twocode", ArrAcountInfo[2]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"server", ArrAcountInfo[3]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"time", NumberToString(i * 2));
				}
				i++;
			}
			sFile.Close();
		}
	}
}




int GetCodeIndex()
{
	// TODO: 在此处添加实现代码.
	CTime tm;
	tm = CTime::GetCurrentTime();
	CString sTime1 = tm.Format("%H");
	for (size_t i = 0; i < 12; i++)
	{
		
		if (_wtoi(g_main_cwnd->m_list_info.GetItemText(i, 5)) == _wtoi(sTime1) || _wtoi(sTime1) - 1 == _wtoi(g_main_cwnd->m_list_info.GetItemText(i, 5))) {
			return i;
		}

	}
	if (_wtoi(sTime1) - 1 == -1) {
		return 0;
	}
	return 0;
}

void CGameCodeDlg::AppendText(CString str)
{
	((CEdit*)GetDlgItem(IDC_EDIT1))->SetSel(GetDlgItem(IDC_EDIT1)->GetWindowTextLength(), GetDlgItem(IDC_EDIT1)->GetWindowTextLength());
	((CEdit*)GetDlgItem(IDC_EDIT1))->ReplaceSel(str + L"\r\n");
}

void CGameCodeDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	//CString str;
	//GetDlgItemText(IDC_EDIT2, str);
	//CStringArray ArrAcountInfo;

	//str.Replace(_T("----"), _T("-"));
	//str.Replace(_T("---"), _T("-"));
	//str.Replace(_T("--"), _T("-"));

	//StringSplit(str, ArrAcountInfo, '-');
	CString str;
	str.Format(L"上号索引%d", GetCodeIndex());
	g_main_cwnd->AppendText(str);
}


void CGameCodeDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	long x, y;
	g_dm->FindPic(0, 0, 1280, 960, L"ngs.bmp", L"050505", 0.9, 0, &x, &y);
	CString str;
	str.Format(L"x=%d,y=%d",x,y);
	g_main_cwnd->AppendText(str);
	if (x > 0) {
		g_dm->MoveTo(x, y);
		g_dm->LeftClick();
		g_dm->KeyPress(13);
	}
}
